require('../../../lib/md5/md5.js');

define(['vue','config','common'],function(Vue,config,utils){
	'use strict';

	var registerView=new Vue({
		el:'#app',
		data:{
			role : 'E享会员',
			recommendPerson:'',
			userType:'',
			userId:'',
			customerID:5,				//商家ID
			secondOfDelay : 120000 ,
			secondLeftOfDelay : 0,
			phone:'',
		},
		created:function(){
			var _this=this;
			//utils.clearData();
			
			var userCode;
			_this.userCode = utils.getQueryVariable('userCode');
			if(_this.userCode !== ""){
				var data = {
					userCode : _this.userCode,
				};

				$.ajax({
	                url: utils.getWebUrl('user/personal'),
	                type: 'POST',
	                data: data, 
	                dataType: 'json',
	                contentType:"application/x-www-form-urlencoded; charset=utf-8",
	                xhrFields: {
	                    withCredentials: true
	                },
	                crossDomain: true,
	                beforeSend:function(){
	                	$('.DataLoading').show();
	                },
	                complete:function(){
	                	$('.DataLoading').hide();
	                },
	                success: function(res) {
	                    if(res.code === "10000"){
	                        if(res.data){
								if(res.data.userName!==null){
									_this.recommendPerson = res.data.userName;
								}else{
									_this.recommendPerson = res.data.userCode;
								}
								_this.userId = res.data.userId;
							}
	                    }else{
	                        utils.appAlert(res.msg,function(){
	                        	window.location.href="NoInvitation.html";
	                        })
	                    }
	                },
	                error: function(res) {
	                    utils.appAlert('系统出现了错误,系统管理员正在修复',function(){
	                    	window.location.href="NoInvitation.html";
	                    });
	                }
	            });
				
			}else{
				window.location.href="NoInvitation.html";
			}
			//获取用户类型
			/*utils.initAjax('user/getUserType',null,'POST',function(res){
				//console.log(res)
				_this.userID = res.data[1].id;
				_this.customerID = res.data[2].id;
			},function(){
				$('.Pop_loading').show();
			},function(){
				$('.Pop_loading').hide();
			})*/
			
		},
		methods:{
			/*SelectRole:function(event){
				$('.Pop_BJ').show();
			},
			Role:function(event){
				this.role=$(event.target).text();
				$('.Pop_BJ').hide();
			},*/
			focus:function(){
				var phone = $('#phone').val();
				if($.trim(phone)!==""){
					$('.delect').fadeIn(300);
				}
			},
			keyup:function(event){
				var phone = $('#phone').val();
				if($.trim(phone)!==""){
					$('.delect').fadeIn(300);
				}else{
					$('.delect').fadeOut(300);
				}			
			},
			blur:function(){
				$('.delect').fadeOut(300);
			},
			delect:function(){
				$('#phone').val('');
				$('.delect').fadeOut(300);
				var phone = $('#phone').val();
				if($.trim(phone) == ""){
					$('#phone').focus();
				}
			},	
			getVeryCode:function(){
				var role = $('#role').text();
				var phone = $('#phone').val();
				var psw = $('#psw').val();
				var _this=this;

				if($.trim(phone) === ""){
                    utils.appAlert('手机号码不能为空',function(){
                    	$('#phone').focus();
                    	$('.framework7-root').css({'overflow-y':'scroll'});
                    });
                    return;
				}

				if(!(/^1(3|4|5|7|8)\d{9}$/.test(phone))){
                    utils.appAlert('请输入正确的手机号码',function(){
                    	$('#phone').focus();
                    	$('.framework7-root').css({'overflow-y':'scroll'});
                    });
                    return;
				}

				utils.initAjax('user/isEixt',{
					phone: phone,
				},'POST',function(res){
					if(res.msg === "true"){
						utils.appAlert('此用户已经存在');
					}else{
						if($.trim(psw) === ""){
		                    utils.appAlert('密码不能为空',function(){
		                    	$('#psw').focus();
		                    	$('.framework7-root').css({'overflow-y':'scroll'});
		                    });
		                    return;
						}
						if(!(/^.{6,20}$/.test(psw))){
							utils.appAlert('密码只能在6~20位之间',function(){
		                    	$('#psw').focus();
		                    	$('.framework7-root').css({'overflow-y':'scroll'});
		                    });
		                    return;
						}
						
						var data = {
							phone: phone,
							codeType: 1,
							smsType: 1
						};
						utils.initAjax('verify/sendPhoneCode',data,'POST',function(res){
					        utils.appAlert('请查收你的短信',function(){
					        	_this.sendVerifyCodeTimer();
					        });
					        $(".verification").attr('disabled','true');
						})
					}
				},function(){
					$('.DataLoading').show();
				},function(){
					$('.DataLoading').hide();
				})
			},

			getvoiceMessage:function(){
				var _this = this;
				var role = $('#role').text();
				var phone = $('#phone').val();
				var psw = $('#psw').val();

				if($.trim(phone) === ""){
                    utils.appAlert('手机号码不能为空',function(){
                    	$('#phone').focus();
                    	$('.framework7-root').css({'overflow-y':'scroll'});
                    });
                    return;
				}

				if(!(/^1(3|4|5|7|8)\d{9}$/.test(phone))){
                    utils.appAlert('请输入正确的手机号码',function(){
                    	$('#phone').focus();
                    	$('.framework7-root').css({'overflow-y':'scroll'});
                    });
                    return;
				}

				if($.trim(psw) === ""){
                    utils.appAlert('密码不能为空',function(){
                    	$('#psw').focus();
                    	$('.framework7-root').css({'overflow-y':'scroll'});
                    });
                    return;
				}

				if(!(/^.{6,20}$/.test(psw))){
					utils.appAlert('密码只能在6~20位之间',function(){
                    	$('#psw').focus();
                    	$('.framework7-root').css({'overflow-y':'scroll'});
                    });
                    return;
				}

				var data = {
					phone: phone,
					codeType: 1,
					smsType: 2
				};
				utils.initAjax('verify/sendPhoneCode',data,'POST',function(res){
			        utils.appAlert('你正在使用语音获取验证码<br/>请保持手机通畅');
				})
			},

			sendVerifyCodeTimer:function(){
				var $time_debounce = $('.verification');
				var _this=this;
				_this.setTimeToDefaut = function(secondsOfInterval) {
                	this.secondLeftOfDelay = secondsOfInterval;
            	};

				var secondsOfInterval = this.secondOfDelay;

				var intervalID = setInterval(function() {
	                secondsOfInterval = secondsOfInterval - 1000;
	                $time_debounce.html(" " + secondsOfInterval / 1000 + " 秒 ")
	                if (secondsOfInterval <= 0) {
	                    clearInterval(intervalID);
	                    $(".verification").removeAttr("disabled");
	                    $time_debounce.html("获取验证码");
	                }
	                _this.setTimeToDefaut(secondsOfInterval);
	            }, 1000);
			},

			submit:function(){
				//console.log(this.userId)
				var role = $('#role').text();
				var phone = $('#phone').val();
				var psw = $('#psw').val();
				var veryCode = $('#veryCode').val();

				if($.trim(phone) === ""){
                    utils.appAlert('手机号码不能为空',function(){
                    	$('#phone').focus();
                    	$('.framework7-root').css({'overflow-y':'scroll'});
                    });
                    return;
				}

				if(!(/^1(3|4|5|7|8)\d{9}$/.test(phone))){
                    utils.appAlert('请输入正确的手机号码',function(){
                    	$('#phone').focus();
                    	$('.framework7-root').css({'overflow-y':'scroll'});
                    });
                    return;
				}

				var _this=this;
				var user_type;

				utils.initAjax('user/isEixt',{
					phone: phone,
				},'POST',function(res){
						if(res.msg === "true"){
							utils.appAlert('此用户已经存在');
						}else{
							if($.trim(psw) === ""){
		                    utils.appAlert('密码不能为空',function(){
		                    	$('#psw').focus();
		                    	$('.framework7-root').css({'overflow-y':'scroll'});
		                    });
		                    return;
						}

						if(!(/^.{6,20}$/.test(psw))){
							utils.appAlert('密码只能在6~20位之间',function(){
		                    	$('#psw').focus();
		                    	$('.framework7-root').css({'overflow-y':'scroll'});
		                    });
		                    return;
						}

						if($.trim(veryCode) === ""){
		                    utils.appAlert('验证码不能为空',function(){
		                    	$('#veryCode').focus();
		                    	$('.framework7-root').css({'overflow-y':'scroll'});
		                    });
		                    return;
						}

						if($('#checkbox')[0].checked === false){
		                    utils.appAlert('请勾选同意《E享时代注册协议》');
		                    $('.framework7-root').css({'overflow-y':'scroll'});
		                    return;
						}

						
						/*if(role.indexOf('会员') === -1){
							user_type = _this.customerID;
						}else{
							user_type = _this.userID;
						}*/

						var data={
							user_id:_this.userId,
							login_name:phone,
							password:psw,
							phone_code:veryCode
						};

						utils.initAjax('user/register',data,'POST',function(res){
							utils.clearData();
							//var userTypeId = res.data.data.userTypeId*1;
							var token = res.data.token;
							utils.userToken(token);
							window.location.href="userInfo.html";		
						},null,function(){
							$('.DataLoading').hide();
						})
					}
				},function(){
					$('.DataLoading').show();
				})
				
			}
		}
	})

	return registerView
});